package hp.com.servlet.OrderServlet;

import hp.com.entity.Orders;
import hp.com.entity.Users;
import hp.com.service.OrderService;
import hp.com.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Orders")
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否登录且获取对象
        HttpSession session = req.getSession();
        //判断是否登录且获取对象
        if(session.getAttribute("userSession")==null){
            req.getRequestDispatcher(req.getContextPath()+"ManageSystem/User/login.jsp").forward(req,resp);
            resp.sendRedirect(req.getContextPath()+"/Login");
        }
        //通过session获取用户对象
        try{
            Users user = (Users) session.getAttribute("userSession");
        OrderService service = new OrderServiceImpl();
        //获取Order对象数组
        List<Orders> orders = service.GetOrderByUserId(user.getId());
        req.setAttribute("ordersList",orders);
        req.setAttribute("user",user);
        req.getRequestDispatcher(req.getContextPath()+"ManageSystem/User/UserOrders.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
